import { WorkflowPredefineDb } from '/@/enums/flowManage';
export const fieldNameOptions = [
  {
    value: 'cws_status',
    label: '记录状态',
  },
  {
    value: 'cws_flag',
    label: '冲抵状态',
  },
  {
    value: 'cws_id',
    label: '关联字段',
  },
  {
    value: 'cws_role',
    label: '用户的角色',
  },
  {
    value: 'cws_cur_user',
    label: '当前用户',
  },
];
export const columnNameOptions1 = [
  {
    value: '-5',
    label: '未开始',
  },
  {
    value: '0',
    label: '处理中',
  },
  {
    value: '1',
    label: '已结束',
  },
  {
    value: '-2',
    label: '已拒绝',
  },
  {
    value: '-3',
    label: '已放弃',
  },
];
export const columnNameOptions2 = [
  {
    value: '0',
    label: '未冲抵',
  },
  {
    value: '1',
    label: '已冲抵',
  },
];
export const dropdownMenuOptions = [
  {
    value: '{$curDate}',
    label: '当前日期',
  },
  {
    value: '{$curYear}',
    label: '当前年份',
  },
  {
    value: '{$curMonth}',
    label: '当前月份',
  },
  {
    value: '{$curUser}',
    label: '当前用户',
  },
  {
    value: '{$curUserDept}',
    label: '当前用户所在的部门',
  },
  {
    value: '{$curUserDeptAndChildren}',
    label: '当前用户所在的部门及其子部门',
  },
  {
    value: '{$curUserRole}',
    label: '当前用户的角色',
  },
  {
    value: '{$admin.dept}',
    label: '当前用户分管的部门',
  },
  {
    value: '{$mainId}',
    label: '主表ID',
  },
];
export const firstBracketOptions = [
  {
    value: '',
    label: '',
  },
  {
    value: '(',
    label: '(',
  },
  {
    value: ')',
    label: ')',
  },
];

export const compareOptions = [
  {
    value: '=',
    label: '=',
  },
  {
    value: '>=',
    label: '>=',
  },
  {
    value: '<=',
    label: '<=',
  },
  {
    value: '>',
    label: '>',
  },
  {
    value: '<',
    label: '<',
  },
  {
    value: '<>',
    label: '<>',
  },
];
export const compareOptions2 = [
  {
    value: '=',
    label: '=',
  },
  {
    value: '<>',
    label: '<>',
  },
  {
    value: '=>',
    label: '=>',
  },
];
export const compareOptions3 = [
  {
    value: '=',
    label: '=',
  },
  {
    value: '<>',
    label: '<>',
  },
];
export const compareOptions4 = [
  {
    value: '=',
    label: '=',
  },
];

export const logicalOptions = [
  {
    value: 'and',
    label: '并且',
  },
  {
    value: 'or',
    label: '或者',
  },
];

export const columns = [
  {
    title: '条件',
    dataIndex: 'tztj',
    key: 'tztj',
    align: 'center',
    slots: { customRender: 'tztj' },
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    key: 'action',
    slots: { customRender: 'action' },
    width: 100,
  },
];

export const random = () => {
  return Math.random();
};

//解析配置 xml
export const handleXML = (condProps) => {
  const linkProps = condProps;
  const xml = $.parseXML(linkProps);
  const $xml = $(xml);
  const linkList: any = [];
  try {
    $xml
      .find('items')
      .children()
      .each(function (i) {
        const obj = {
          id: $(this).attr('id'),
          firstBracket: $(this).find('firstBracket').text(),
          fieldName: $(this).find('fieldName').text(),
          mainColumn: $(this).find('name').text(),
          compare: $(this).find('operator').text(),
          value: $(this).find('value').text(),
          twoBracket: $(this).find('twoBracket').text(),
          logical: $(this).find('logical').text(),
          fieldType: $(this).find('fieldType').text(),
          columnName: $(this).find('value').text(),
          role: '',
          userDept: '',
        };
        // > < &gt; &lt;
        obj.compare = obj.compare.replaceAll('&lt;', '<');
        obj.compare = obj.compare.replaceAll('&gt;', '>');
        if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_FIELD) {
          obj['columnName'] = obj.value;
        }
        if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_PRIV_ROLE) {
          obj['role'] = obj.value;
        }
        if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_PRIV_DEPT) {
          obj['userDept'] = obj.value;
        }
        obj['columnNameTitle'] = dropdownMenuOptions.find((item) => item.value == obj.value)?.label;
        for (const v in obj) {
          if (obj[v] == null || obj[v] == 'null') {
            obj[v] = '';
          }
        }
        linkList.push(obj);
      });
  } catch (err) {
    console.log('links', err);
  }
  return linkList;
};

export const setXML = (list) => {
  const links = '<items>';
  let str = '';
  let val = '';
  const len = list.length;
  // const maxIndex = $('#maxIndex').val();
  // const currentMaxIndex = parseInt($('#currentMaxIndex').val()) + 1;
  // if (currentMaxIndex > maxIndex) {
  //   len = currentMaxIndex;
  // } else {
  //   if (maxIndex > len) {
  //     len = maxIndex;
  //   }
  // }

  for (let i = 0; i < len; i++) {
    const obj = list[i];
    if (obj != null) {
      const id = random();
      const link = '<item id="' + id + '">';
      const mainColumn = '<name>' + obj.mainColumn + '</name>';
      const firstBracket = '<firstBracket>' + obj.firstBracket + '</firstBracket>';
      const fieldName = '<fieldName>' + obj.fieldName + '</fieldName>';
      let compare = obj.compare;
      // > < &gt; &lt;
      compare = compare.replaceAll('<', '&lt;');
      compare = compare.replaceAll('>', '&gt;');
      const operator = '<operator>' + compare + '</operator>';
      const logical = '<logical>' + obj.logical + '</logical>';
      const twoBracket = '<twoBracket>' + obj.twoBracket + '</twoBracket>';

      // if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_FIELD) {
      //   if ($('#columnName' + i).attr('val')) {
      //     val = '<value>' + $('#columnName' + i).attr('val') + '</value>';
      //   } else {
      //     val = '<value>' + $('#columnName' + i).val() + '</value>';
      //   }
      // }

      if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_FIELD) {
        val = '<value>' + obj.columnName + '</value>';
      } else if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_PRIV_ROLE) {
        val = '<value>' + obj.role + '</value>';
      } else if (obj.mainColumn == WorkflowPredefineDb.COMB_COND_TYPE_PRIV_DEPT) {
        val = '<value>' + obj.userDept + '</value>';
      } else {
        val = '<value>' + obj.columnName + '</value>';
      }
      const fieldType = '<fieldType>' + obj.fieldType + '</fieldType>';
      const endLink = '</item>';
      str +=
        link +
        firstBracket +
        fieldName +
        mainColumn +
        operator +
        val +
        twoBracket +
        logical +
        fieldType +
        endLink;
    }
  }
  const endLinks = '</items>';
  if (!str) {
    return '';
  } else {
    return links + str + endLinks;
  }
};
